package com.ruoyi.web.controller.saleManagement;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.saleManagement.saleRegisterInfo.domain.SaleRegisterInfo;
import com.ruoyi.saleManagement.saleRegisterInfo.service.ISaleRegisterInfoService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.storage.bicycle.service.IBicycleService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Yuan
 * @description
 * @date 2024/3/17
 */
@RestController
@RequestMapping("/saleManagement/register")
@RequiredArgsConstructor
public class ClientSaleRegisterController {
    private static final Logger log = LoggerFactory.getLogger(ClientSaleRegisterController.class);
    private final IBicycleService bicycleService;
    private final ServerConfig serverConfig;
    private final ISaleRegisterInfoService saleRegisterInfoService;

    /**
     * 判断订单是否存在
     */
    @GetMapping("/Exist")
    @RateLimiter(time = 30, count = 100)
    public AjaxResult isExist(String keyWord)
    {
        return bicycleService.selectBicycleExist(keyWord);
    }
    /**
     * 根据ID查询详细信息
     */
    @GetMapping("/getOrderDetail")
    @RateLimiter(time = 30, count = 100)
    public AjaxResult getOrderDetail(String keyWord)
    {
        return bicycleService.selectBicycleById(keyWord);
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/uploadPicture")
    @RateLimiter(time = 30, count = 100)
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file, MimeTypeUtils.IMAGE_EXTENSION);
            String url = serverConfig.getUrl() + fileName;
            return AjaxResult.success(url);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    //保存登记信息的接口
    @PostMapping("/saveRegister")
    @RateLimiter(time = 30, count = 100)
    public AjaxResult saveRegister(@RequestBody SaleRegisterInfo saleRegisterInfo) {
        saleRegisterInfo.setSource("01");
        int i = saleRegisterInfoService.insertSaleRegisterInfo(saleRegisterInfo);
        if(i > 0) {
            return AjaxResult.success("提交成功");
        } else {
            return AjaxResult.error("提交失败");
        }
    }

    //根据订单号获取配件信息，再根据配件信息获取登记数据
    @GetMapping("/getRegisterInfo")
    @RateLimiter(time = 30, count = 100)
    public AjaxResult getRegisterInfo(String bicycleId) {
        return saleRegisterInfoService.getRegisterInfo(bicycleId);
    }

}
